package com.xuqi.DataPool;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferenceDataPool implements IInspectDataPool
{

	private SharedPreferences mShareData = null;
	private SharedPreferences.Editor mShareDataEditor = null;

	public SharedPreferenceDataPool(String name, Context c)
	{
		if (c != null)
			mShareData = c.getSharedPreferences(name, 0);
		if (mShareData != null)
			mShareDataEditor = mShareData.edit();
	}

	@Override
	public boolean putBoolean(String key, boolean val)
	{
		if (mShareDataEditor != null)
		{
			mShareDataEditor.putBoolean(key, val);
			return mShareDataEditor.commit();
		}
		return false;
	}

	@Override
	public boolean putFloat(String key, float val)
	{
		if (mShareDataEditor != null)
		{
			mShareDataEditor.putFloat(key, val);
			return mShareDataEditor.commit();
		}
		return false;
	}

	@Override
	public boolean putInt(String key, int val)
	{
		if (mShareDataEditor != null)
		{
			mShareDataEditor.putInt(key, val);
			return mShareDataEditor.commit();
		}
		return false;
	}

	@Override
	public boolean putLong(String key, long val)
	{
		if (mShareDataEditor != null)
		{
			mShareDataEditor.putLong(key, val);
			return mShareDataEditor.commit();
		}
		return false;

	}

	@Override
	public boolean putString(String key, String val)
	{
		if (mShareDataEditor != null)
		{
			mShareDataEditor.putString(key, val);
			return mShareDataEditor.commit();
		}
		return false;

	}

	@Override
	public boolean getBoolean(String key, boolean defaultval)
	{
		if (mShareData != null)
		{
			return mShareData.getBoolean(key, defaultval);
		}
		return defaultval;
	}

	@Override
	public float getFloat(String key, float defaultval)
	{
		if (mShareData != null)
		{
			return mShareData.getFloat(key, defaultval);
		}
		return defaultval;
	}

	@Override
	public int getInt(String key, int defaultval)
	{
		if (mShareData != null)
		{
			return mShareData.getInt(key, defaultval);
		}
		return defaultval;
	}

	@Override
	public long getLong(String key, long defaultval)
	{
		if (mShareData != null)
		{
			return mShareData.getLong(key, defaultval);
		}
		return defaultval;
	}

	@Override
	public String getString(String key, String defaultval)
	{
		if (mShareData != null)
		{
			return mShareData.getString(key, defaultval);
		}
		return defaultval;
	}

	@Override
	public void clear()
	{
		if (mShareDataEditor != null)
		{
			mShareDataEditor.clear();
			mShareDataEditor.commit();
		}
	}

}
